package com.framework.utils.pay.youfupay.method;

import com.alibaba.fastjson.JSON;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.utils.jiufu.StringUtils;
import com.framework.utils.pay.suixingfu.horfu.util.Base64Utils;
import com.framework.utils.pay.suixingfu.horfu.util.DESUtils;
import com.framework.utils.pay.suixingfu.horfu.util.JsonUtils;
import com.framework.utils.pay.suixingfu.horfu.util.RSAUtils;
import com.framework.utils.pay.youfupay.entity.AccountModle;
import com.framework.utils.pay.youfupay.entity.RequestMessage;
import com.framework.utils.pay.youfupay.util.Constant;
import com.framework.utils.pay.youfupay.util.HttpClientHelper;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author wq
 * @date 2019/10/9 16:07
 * @description
 */
@Slf4j
public class AccountQueryMethod {

    //static String url = "http://api.clooudnext.com:8280/pctrans-business/clientBusiness/common.do";
    static String url = "http://api.youfupingtai.com/clientBusiness/common.do";
    //static String url = "http://apitest.youfupingtai.com/pctrans-business/clientBusiness/common.do";

    public static BigDecimal accountQuery(String secretKey, SysPayment payment, String levyId) {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("req" + System.currentTimeMillis());
        //接口编码
        rm.setFunCode("6003");
        //企业商户编码
        rm.setMerId(payment.getClientId());
        //版本信息
        rm.setVersion("V1.0");
        String json = toJson(levyId);
        RequestMessage requestMessage = null;
        String detailData = null;
        try {
            byte[] bs = DESUtils.encrypt(json.getBytes(Constant.CHARSET), secretKey);

            String reqDataEncrypt = Base64Utils.encode(bs);
            //请求数据
            rm.setReqData(reqDataEncrypt);
            //签名
            rm.setSign(RSAUtils.sign(reqDataEncrypt, payment.getPrivateKey()));
            String reqStr = JsonUtils.toJson(rm);
            System.out.println(reqStr);

            Map<String, Object> map = new HashMap<>(1);
            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, Constant.CHARSET);
            System.out.println("resData:" + resData);

            requestMessage = JsonUtils.fromJson(resData, RequestMessage.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isBlank(requestMessage.getSign())) {
            log.info(requestMessage.getResMsg());
            throw new RRException("9000", "提现系统错误,请稍后再试");
        }
        try {
            if (!RSAUtils.verify(requestMessage.getResData(), requestMessage.getSign(), payment.getPublicKey())) {
                throw new RRException("9000", "解密错误");
            }


            byte[] base64bs = Base64Utils.decode(requestMessage.getResData());
            byte[] debs = DESUtils.decrypt(base64bs, secretKey);
            detailData = new String(debs, Constant.CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("detailData:" + detailData);

        Map<String, Object> result = JSON.parseObject(detailData, Map.class);

        Map<String, Object> accountMap = (Map<String, Object>) result.get("accountMap");

        BigDecimal total = new BigDecimal("0.00");

        if (accountMap.isEmpty()) {
            throw new RRException("9000", "当前提现人数过多，请稍后再试");
        } else {
            Set<String> strings = accountMap.keySet();

            Iterator<String> it = strings.iterator();
            while (it.hasNext()) {
                String key = it.next();
                total = total.add(new BigDecimal(accountMap.get(key).toString()).divide(new BigDecimal("100").setScale(2)));
            }
            System.out.println(total);
        }

        return total;
    }

    private static String toJson(String levyId) {
        AccountModle am = new AccountModle();
        am.setLevyId(Long.valueOf(levyId));
        return JSON.toJSONString(am);
    }

}
